package com.demo2do.fms.admin.entity.search.supplier;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;
import com.demo2do.fms.entity.vehicle.source.LockType;
import com.demo2do.fms.entity.vehicle.source.SyncStatus;

/**
 *
 * @author Ark
 *
 */
public class VehicleSourceSearch implements QueryBuilder {
	
	/** 多个牵引车中文车牌号 */
	private List<String> tractorBrandCodes;
    
    /** 牵引车中文车牌 */
    private String tractorBrandCode;
    
    /** 挂车中文车牌 */
    private String trailerBrandCode;
    
    /** 同步状态 */
    private SyncStatus status;
    
    /** 所属供方 */
    private Long supplierId;
    
    /** 所属供方代码 */
    private String supplierCode;
    
    /** 所属供方名称 */
    private String supplierName;
    
    /** 是否已绑定司机 */
    private Boolean bind;
    
    /** 车辆是否同步 */
    private Boolean synchronize;
    
    /** 是否锁定 */
    private Boolean lock;
    
    /** 锁定类型 */
    private LockType lockType;
    
    private QueryOrder queryOrder;
    
    private QueryFilter queryFilter;
    
    /**
     * The default constructor
     */
    public VehicleSourceSearch() {
        queryOrder = new QueryOrder("tractorBrandCode", "ASC");
    }

    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public QueryFilter getPreparedFilter(boolean refresh) {
        
        if (queryFilter == null || refresh) {
            
            queryFilter = new QueryFilter().addLike("tractorBrandCode", tractorBrandCode)
                                           .addLike("trailerBrandCode", trailerBrandCode)
                                           .addEquals("status", status)
                                           .addEquals("supplier.id", supplierId)
                                           .addEquals("supplier.code", supplierCode)
                                           .addLike("supplier.name", supplierName)
                                           .addEquals("synchronize", synchronize)
                                           .addEquals("lock", lock)
                                           .addEquals("lockType", lockType);
            
            if (CollectionUtils.isNotEmpty(tractorBrandCodes)) {
            	queryFilter.addSentence("tractorBrandCode IN (:tractorBrandCodes)", "tractorBrandCodes", tractorBrandCodes);
            }
            
        }
        
        return queryFilter;
        
    }
    
    /**
     * @param supplierIds
     */
    public void addSupplierId(List<Long> supplierIds) {
        supplierIds.add(0L);
        this.getPreparedFilter(false).addSentence("supplier.id IN (:supplierIds)", "supplierIds", supplierIds);
    }
    
    /**
     * vehicle already bind driver
     */
    public void addBindDriver() {
    	this.getPreparedFilter(false).addSentence("FC_GET_DRIVER_AMOUNT(code) > 0", new HashMap<String, Object>());
    }
    
    /**
     * vehicle no bind driver
     */
    public void addUnbindDriver() {
    	this.getPreparedFilter(false).addSentence("FC_GET_DRIVER_AMOUNT(code) = 0", new HashMap<String, Object>());
    }

    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public QueryOrder getOrder() {
        return queryOrder;
    }

    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        String sentence = "FROM VehicleSource WHERE disabled = false" + getPreparedFilter(false).clauseSentence() + queryOrder.sentence();
        return sentence;
    }

    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).parameters();
    }
    
    /**
     * @return
     */
    public QueryFilter getQueryFilter() {
        return getPreparedFilter(false);
    }

    /**
     * @return the tractorBrandCodes
     */
    public List<String> getTractorBrandCodes() {
        return tractorBrandCodes;
    }

    /**
     * @return the tractorBrandCode
     */
    public String getTractorBrandCode() {
        return tractorBrandCode;
    }

    /**
     * @return the trailerBrandCode
     */
    public String getTrailerBrandCode() {
        return trailerBrandCode;
    }

    /**
     * @return the status
     */
    public SyncStatus getStatus() {
        return status;
    }

    /**
     * @return the supplierId
     */
    public Long getSupplierId() {
        return supplierId;
    }

    /**
     * @return the supplierCode
     */
    public String getSupplierCode() {
        return supplierCode;
    }

    /**
     * @return the supplierName
     */
    public String getSupplierName() {
        return supplierName;
    }

    /**
     * @return the bind
     */
    public Boolean getBind() {
        return bind;
    }

    /**
     * @return the synchronize
     */
    public Boolean getSynchronize() {
        return synchronize;
    }

    /**
     * @return the lock
     */
    public Boolean getLock() {
        return lock;
    }

    /**
     * @return the lockType
     */
    public LockType getLockType() {
        return lockType;
    }

    /**
     * @param tractorBrandCodes the tractorBrandCodes to set
     */
    public void setTractorBrandCodes(List<String> tractorBrandCodes) {
        this.tractorBrandCodes = tractorBrandCodes;
    }

    /**
     * @param tractorBrandCode the tractorBrandCode to set
     */
    public void setTractorBrandCode(String tractorBrandCode) {
        this.tractorBrandCode = StringUtils.trim(tractorBrandCode);
    }

    /**
     * @param trailerBrandCode the trailerBrandCode to set
     */
    public void setTrailerBrandCode(String trailerBrandCode) {
        this.trailerBrandCode = StringUtils.trim(trailerBrandCode);
    }

    /**
     * @param status the status to set
     */
    public void setStatus(SyncStatus status) {
        this.status = status;
    }

    /**
     * @param supplierId the supplierId to set
     */
    public void setSupplierId(Long supplierId) {
        this.supplierId = supplierId;
    }

    /**
     * @param supplierCode the supplierCode to set
     */
    public void setSupplierCode(String supplierCode) {
        this.supplierCode = supplierCode;
    }

    /**
     * @param supplierName the supplierName to set
     */
    public void setSupplierName(String supplierName) {
        this.supplierName = supplierName;
    }

    /**
     * @param bind the bind to set
     */
    public void setBind(Boolean bind) {
        this.bind = bind;
    }

    /**
     * @param synchronize the synchronize to set
     */
    public void setSynchronize(Boolean synchronize) {
        this.synchronize = synchronize;
    }

    /**
     * @param lock the lock to set
     */
    public void setLock(Boolean lock) {
        this.lock = lock;
    }

    /**
     * @param lockType the lockType to set
     */
    public void setLockType(LockType lockType) {
        this.lockType = lockType;
    }

}